﻿using System;
using System.Collections.Generic;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Core;
using eBlog.Entity;
using eBlog.Service;
namespace eBlog.Web
{
    public partial class Post : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Init_Template();
                TemplateUrl = "post.html";
                LoadPost();
                this.WriteHTML();
            }

        }

        /// <summary>
        /// 加载文章
        /// </summary>
        protected void LoadPost()
        {
            dict.Put(TagFields.IS_POST, "1");

            if (RequestHelper.IsPost && !RequestHelper.IsCrossSitePost)
            {
               // AddComment();
            }

            Posts post = null;

            int postId = -1;
            string pid = RequestHelper.QueryString("id");

            if (StringHelper.IsInt(pid))
            {
                post = PostsService.GetPostsById(int.Parse(pid));
            }
            //else
            //{
            //    post = PostManager.GetPost(StringHelper.SqlEncode(name));
            //}

            if (post == null)
            {
                Response.Redirect(ConfigHelper.SiteUrl);
            }

            if (post.Status == (int)PostStatus.Draft)
            {
               // ResponseError("文章未发布", "囧！此文章未发布！");
            }

            string cookie = "isviewpost" + post.ID;
            int isview = StringHelper.StrToInt(CookiePer.GetCookie(cookie), 0);
            //未访问或按刷新统计
            if (isview == 0 || SettingService.GetSetting().SiteTotalType == 1)
            {
                PostsService.UpdatePostViewCount(post.ID, 1);
            }
            //未访问
            if (isview == 0 && SettingService.GetSetting().SiteTotalType == 2)
            {
                CookiePer.SetCookie(cookie, "1", 1440);
            }
            string author = "Admin";
            
            //if (post.UserID != null)
            //{
            //  author= UserService.GetUserById(post.UserID.Value).LoginName;

            //}
            
            dict.Put(TagFields.POSTS_AUTHOR,author);
            dict.Put(TagFields.POST, post);
            dict.Put(TagFields.PAGE_TITLE, post.Title);
            dict.Put(TagFields.Post_TAGS,TermsService.GetPostTagsList(post.ID));
           
            string metaKeywords = string.Empty;
            //foreach (TagInfo tag in post.Tags)
            //{
            //    metaKeywords += tag.Name + ",";
            //}
            //if (metaKeywords.Length > 0)
            //{
            //    metaKeywords = metaKeywords.TrimEnd(',');
            //}
            //th.Put(TagFields.META_KEYWORDS, metaKeywords);

            string metaDescription = post.Summary;
            if (string.IsNullOrEmpty(post.Summary))
            {
                metaDescription = post.Content;
            }
            dict.Put(TagFields.META_DESCRIPTION, StringHelper.CutString(StringHelper.RemoveHtml(metaDescription), 50).Replace("\n", ""));

            int recordCount = 0;
            List<Comments> commentList = CommentService.GetCommentList(SettingService.GetSetting().PageSizeCommentCount, 1, out recordCount, SettingService.GetSetting().CommentOrder, -1, post.ID, 0, -1, -1, null);
            dict.Put(TagFields.COMMENTS, commentList);
            //th.Put(TagFields.PAGER, Pager.CreateHtml(SettingManager.GetSetting().PageSizeCommentCount, recordCount, post.PageUrl + "#comments"));

           //// 同时判断评论数是否一致
           // if (recordCount != post.CommentCount)
           // {
           //     post.CommentCount = recordCount;
           //     PostsService.UpdatePost(post);

           // }

           
        }


    }
}